import { BasicColumn, FormSchema } from '/@/components/Table';
import { h } from "vue"
import { render } from '/@/utils/common/renderUtils';
import { DescItem } from '/@/components/Description/index';
import { useI18n } from '/@/hooks/web/useI18n';
import AddInput from "/@/components/Form/src/jeecg/components/JAddInput.vue"
import * as evbApi from '/@/api/infra/eventBus';
const { t } = useI18n('');

export const columns: BasicColumn[] = [
  {
    title: t('订阅编号'),
    dataIndex: 'id',
    width: 130,
  },
  {
    title: t('订阅主题'),
    dataIndex: 'topicName',
    width: 130,
  },
  {
    title: t('发布者'),
    dataIndex: 'publisherName',
    width: 130,
  },
  {
    title: t('订阅者'),
    dataIndex: 'subscriberName',
    width: 130,
  },
  {
    title: t('状态'),
    dataIndex: 'status',
    width: 100,
    customRender: ({ text }) => {
      const color = text == '1' ? "green" : "red"
      return render.renderTag(text == '1' ? "启用" : "禁用", color)
    },
  },
  {
    title: t('创建时间'),
    dataIndex: 'updatedAt',
    width: 150,
    align: 'center',
  }
];

export const searchFormSchema: FormSchema[] = [
  {
    label: t('发布者'),
    field: 'publisherCode',
    component: 'ApiSelect',
    componentProps: {
      api: evbApi.listPublisher,
      showSearch: true,
      allowClear: true,
      labelField: 'name',
      valueField: 'code',
    },
  },
  {
    label: t('订阅者'),
    field: 'subscriberCode',
    component: 'ApiSelect',
    componentProps: {
      api: evbApi.listSubscriber,
      showSearch: true,
      allowClear: true,
      labelField: 'name',
      valueField: 'code',
    },
  },
  {
    label: t('主题标识'),
    field: 'topicCode',
    component: 'Input',
  },
];


export const inputFormSchemas: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: t('订阅者'),
    required: true,
    field: 'subscriberCode',
    component: 'ApiSelect',
    componentProps: {
      api: evbApi.listSubscriber,
      showSearch: true,
      allowClear: true,
      labelField: 'name',
      valueField: 'code',
    },
  },
  {
    label: t('订阅主题'),
    required: true,
    field: 'topicCode',
    component: 'ApiSelect',
    componentProps: {
      api: evbApi.listTopic,
      showSearch: true,
      allowClear: true,
      labelField: 'name',
      valueField: 'code',
    },
  },
  {
    label: t('状态'),
    required: true,
    field: 'status',
    defaultValue: '1',
    component: "JDictSelectTag",
    componentProps: {
      dictType: "data_use_status",
      type: 'radio'
    },
  },
  {
    label: t('请求头'),
    required: false,
    field: 'extraHeader',
    component: "JAddInput",
    helpMessage: "键值对形式填写",
  },
];

export const msgFieldColumns : BasicColumn[] = [
  {
    title: t('名称'),
    dataIndex: 'fieldName',
    align: 'left',
    customRender: ({ text, record }) => {
      if (record.fieldKey == 'ITEMS') {
        return render.renderTag(text, 'green')
      }
      return `${record.fieldName}（${record.fieldKey}）-> ${record.subscribeMapperKey}`
    },
    width: 220,
  },
  {
    title: t('类型'),
    dataIndex: 'fieldType',
    width: 80,
  },
]

export const fieldLibColumns : BasicColumn[] = [
  {
    title: t('名称'),
    dataIndex: 'fieldName',
    align: 'left',
    customRender: ({ text, record }) => {
      if (record.fieldKey == 'ITEMS') {
        return render.renderTag(text, 'green')
      }
      return `${record.fieldName}（${record.fieldKey}）`
    },
    width: 160,
  },
  {
    title: t('类型'),
    dataIndex: 'fieldType',
    width: 100,
  },
]

export const fieldFormSchemas: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '',
    field: 'topicCode',
    component: 'Input',
    show: false,
  },
  {
    label: '',
    field: 'parentId',
    component: 'Input',
    defaultValue: null,
    show: false,
  },
  {
    label: t('字段名称'),
    required: true,
    field: 'fieldName',
    component: 'Input',
    dynamicDisabled: true,
  },
  {
    label: t('字段标识'),
    required: true,
    field: 'fieldKey',
    dynamicDisabled: true,
    component: 'Input',
  },
  {
    label: t('映射标识'),
    required: true,
    field: 'subscribeMapperKey',
    component: 'Input',
  },
  {
    label: t('过滤操作符'),
    field: 'subscribeFilterOperator',
    required: true,
    defaultValue: null,
    component: "JDictSelectTag",
    componentProps: {
      placeholder: "请选择",
      allowClear: true,
      showSearch: true,
      dictType: "evb_subscribeFilterOperator",
    },
  },
  {
    label: t('过滤值'),
    field: 'subscribeFilterValue',
    required: false,
    defaultValue: null,
    ifShow: ({ model, field }) => {
      const value = model["subscribeFilterOperator"]
      return value != 'none'
    },
    component: "Input",
  }
];


export const formDetailSchema: DescItem[] = [
  {
    label: t('订阅编号'),
    field: 'id'
  },
  {
    label: t('订阅主题'),
    field: 'topicName',
    render: (curVal, data) => {
      return `${curVal}（${data.topicCode}）`
    },
  },
  {
    label: t('发布者'),
    field: 'publisherName',
    render: (curVal, data) => {
      return `${curVal}（${data.publisherCode}）`
    },
  },
  {
    label: t('订阅者'),
    field: 'subscriberName',
    render: (curVal, data) => {
      return `${curVal}（${data.subscriberCode}）`
    },
  },
  {
    label: t('状态'),
    field: 'status',
    render: (curVal, data) => {
      return render.renderTag(render.renderDict(curVal, "data_use_status"), curVal == '1' ? "green" : "red")
    },
  },
  {
    label: t('请求头'),
    field: 'extraHeader',
    render: (curVal, _) => {
      return h(AddInput, { value: curVal, readonly: true, showAddButton: false })
    }
  },
  {
    label: t('创建时间'),
    field: 'createdAt'
  },
  {
    label: t('创建者'),
    field: 'createdBy'
  },
  {
    label: t('更新时间'),
    field: 'updatedAt'
  },
  {
    label: t('更新者'),
    field: 'updatedBy'
  }
];
